<?php

// Created by Khanh Nam
class DispatchesController extends Controller
{
    public $layout = '//layouts/customer';

    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'application.filters.RequireLoginFilter',
            ),
        );
    }

    public function actionList($custno, $locno, $page = 0, $sort = NULL, $dir = NULL)
    {
        if ($page > 0) $page--;
        $order = CJSON::decode($sort);
        $year = date('Y') - 1;
        $year = $year.date('-m-j');
	        $criteria = new CDbCriteria();
        $criteria->condition = 'inactive=0';
        $criteria->addCondition("name !='CORP - Internal Dispatches'");
	$criteria->order = isset($order) ? $order[0]['property'] . ' ' . $order[0]['direction'] : NULL;
        $data = new CActiveDataProvider('Dispatchdepartments', array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 20,
                'currentPage' => $page
            )
        ));

        if ($data != NULL && count($data->getData()) > 0) {

            $rs['totalCount'] = $data->getTotalItemCount();
            foreach ($data->getData() as $i => $item) {
                /* var Warehouse $item*/
                $rs['data'][$i]['name'] = $item->name;
                if ($locno != -1)
                    $rs['data'][$i]['open'] = Dispatchdetails::model()->count('custno=:custno AND locno=:locno AND department=:department AND complete IS NULL', array(
                        'custno' => $custno,
                        'locno' => $locno,
                        'department' => $item->name
                    ));
                else   $rs['data'][$i]['open'] = Dispatchdetails::model()->count('custno=:custno AND  department=:department AND  complete IS NULL ', array(
                    'custno' => $custno,
                    'department' => $item->name
                ));
                if ($locno != -1)
                    $rs['data'][$i]['close'] = Dispatchdetails::model()->count('custno=:custno AND locno=:locno AND department=:department AND complete IS NOT NULL AND receiveddate>:year', array(
                        'custno' => $custno,
                        'locno' => $locno,
                        'department' => $item->name,
                        'year' => $year
                    ));
                else
                    $rs['data'][$i]['close'] = Dispatchdetails::model()->count('custno=:custno  AND department=:department AND complete IS NOT NULL AND  receiveddate>:year', array(
                        'custno' => $custno,
                        'year' => $year,
                        'department' => $item->name,
                    ));
            }
            echo json_encode($rs);
        }
        Yii::app()->end();

    }

    public function actionIndex($locno = -1)
    {
        $custno = Yii::app()->user->custno;
        $caller = getUserRealname(Yii::app()->user->name);
        Yii::app()->clientScript->registerScript('caller', "var caller='$caller';", CClientScript::POS_HEAD);

        outputJsByList(Dispatchdetails::model()->getDepartmentList(), 'department');
        outputJsByList(Dispatchdetails::model()->getPriorityList(), 'priority');

        outputJsByList(JobsClass::getList(), 'job');
        $location = Locations::model()->find('custno=:custno AND locno=:locno', array(
            'custno' => $custno,
            'locno' => $locno,
        ));
        $customer = Customers::model()->findByPk($custno);
        $this->render('index', array(
            'location' => $location,
            'customer' => $customer,
        ));
    }
}

?>
